<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :width="1000"
    :title="title"
    :visible="innerVisible"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')"
    :okButtonProps="{style:{display:'none'}}"
  >

    <div class="table-operator" style="border-top: 5px">
      <a-button @click="handleRevertBatch" v-if="selectedRowKeys.length>0" type="primary" icon="plus">{{$t('m.system.user.other.restore_user')}}</a-button>
      <a-button @click="handleDeleteBatch" v-if="selectedRowKeys.length>0" type="default" icon="delete">{{$t('m.system.user.other.physical_delete')}}</a-button>
    </div>
    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      bordered
      :columns="columns"
      :loading="loading"
      :dataSource="dataSource"
      :pagination="false"
      :rowSelection="{selectedRowKeys, onChange: handleTableSelectChange}"
    >

      <!-- 显示头像 -->
      <template slot="avatarslot" slot-scope="text, record, index">
        <div class="anty-img-wrap">
          <a-avatar shape="square" :src="url.getAvatar(record.avatar)" icon="user"/>
        </div>
      </template>

      <span slot="action" slot-scope="text, record">
        <a @click="handleRevert([record.id])" v-has="'system:SysUser:putRecycleBin'"><ych-icon type="redo"/> {{$t('m.system.user.other.restore_user')}}</a>
        <a-divider type="vertical"/>
        <a @click="handleDelete([record.id])" v-has="'system:SysUser:deleteRecycleBin'"><ych-icon type="delete"/> {{$t('m.system.user.other.physical_delete')}}</a>
      </span>
    </a-table>

  </a-modal>
</template>

<script>

  import { putAction,deleteAction,getFileAccessHttpUrl } from "@/api/manage"
  import YchConstant from "@/api/YchConstant";

  function superRequest(options) {
    this.loading = !!options.loading
    options.promise.then(res => {
      if (res.success && typeof options.success === 'function') {
        options.success(res)
      } else {
        throw new Error(res.message)
      }
    }).catch(e => {
      this.messageUtil.warning(this.$t('m.common.tip.operate_error')+'：' + (e.message || e))
    }).finally(() => {
      this.loading = false
    })
  }

  export default {
    name: 'UserRecycleBinModal',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        title: this.$t('m.system.user.other.recycle'),
        loading: false,
        innerVisible: false,
        selectedRowKeys: [],
        dataSource: [],
        columns: [
          { title:this.$t('m.common.label.number'), align: 'center', key: 'rowIndex', width: 80, customRender: (t, r, i) => i + 1 },
          { title: this.$t('m.system.user.entity.username'), align: 'center', dataIndex: 'username' },
          { title: this.$t('m.system.user.entity.realname'), align: 'center', dataIndex: 'realname', },
          // { title: this.$t('m.system.user.entity.avatar'), align: 'center', dataIndex: 'avatar', scopedSlots: { customRender: 'avatarslot' } },
          { title: this.$t('m.system.user.entity.email'), align: 'center', dataIndex: 'email' },
          { title: this.$t('m.system.user.entity.phone'), align: 'center', dataIndex: 'phone' },
          { title: this.$t('m.common.label.operate'), align: 'center', dataIndex: 'action', width: 250, scopedSlots: { customRender: 'action' } }
        ],
        url: {
          getAvatar: (path) => getFileAccessHttpUrl(`${path}`),
          recycleBin: YchConstant.PREFIX_SYSTEM + '/user/recycleBin',
          putRecycleBin: YchConstant.PREFIX_SYSTEM + '/user/putRecycleBin',
          deleteRecycleBin: YchConstant.PREFIX_SYSTEM + '/user/deleteRecycleBin',
        },
      }
    },
    watch: {
      visible: {
        immediate: true,
        handler(val) {
          if (val) {
            this.loadData()
          }
          this.innerVisible = val
        }
      },
      innerVisible(val) {
        this.$emit('update:visible', val)
      },
    },
    methods: {
      loadData() {
        superRequest.call(this, {
          loading: true,
          promise: this.$http.get(this.url.recycleBin),
          success: res => this.dataSource = res.result
        })
      },
      handleOk() {
        this.loadData()
        this.$emit('ok')
      },
      handleCancel() {
        this.innerVisible = false
      },
      // 还原用户
      handleRevert(userIds) {
        this.$confirm({
          title: this.$t('m.system.user.other.restore_user'),
          content: this.$t('m.system.user.verify.restore_user'),
          centered: true,
          onOk: () => {
            putAction(this.url.putRecycleBin,{userIds:userIds.join(',')}).then((res)=>{
              if(res.success){
                this.handleOk()
                this.handleClearSelection()
                this.messageUtil.success(this.$t('m.common.tip.operate_success'))
              }
            })
          }
        })
      },
      // 彻底删除用户
      handleDelete(userIds) {
        this.$confirm({
          title: this.$t('m.system.user.other.physical_delete'),
          content: this.$t('m.system.user.verify.physical_delete'),
          centered: true,
          onOk: () => {
            var that = this;
            deleteAction(that.url.deleteRecycleBin, {userIds: userIds.join(',')}).then((res) => {
              if (res.success) {
                this.loadData()
                this.handleClearSelection()
                this.messageUtil.success(this.$t('m.common.tip.operate_success'))
              } else {
                that.messageUtil.warning(res.message);
              }
            });
          },
        })
      },
      handleRevertBatch() {
        this.handleRevert(this.selectedRowKeys)
      },
      handleDeleteBatch() {
        this.handleDelete(this.selectedRowKeys)
      },
      handleClearSelection() {
        this.handleTableSelectChange([], [])
      },
      handleTableSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectionRows = selectionRows
      },
    }
  }
</script>

<style lang="less" scoped></style>
